package com.itany.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.ResponseCodeConstant;
import com.itany.cms.entity.Chapter;
import com.itany.cms.entity.Course;
import com.itany.cms.entity.CourseType;
import com.itany.cms.exception.*;
import com.itany.cms.service.CourseService;
import com.itany.cms.service.CourseTypeService;
import com.itany.cms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseTypeService typeService;
    @RequestMapping("/findCourse")
    public ModelAndView findCourse(Course courseParam, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        try {
            List<CourseType> enabledTypes = typeService.findEnabled();
            mav.addObject("enabledTypes",enabledTypes);
            PageInfo<Course> coursePage = courseService.findByParams(courseParam,pageNo, pageSize);
            mav.addObject("coursePage", coursePage);
        } catch (CourseTypeNotExistsException e) {
            mav.addObject("errorMsg", e.getMessage());
        }
        mav.setViewName("backend/back_courseSet");
        return mav;
    }
    @RequestMapping("/findChapter")
    public ModelAndView findCourse(@RequestParam("id") String  id, String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();

            Chapter chapterParam = new Chapter();
            chapterParam.setCourseId(Integer.parseInt(id));
            PageInfo<Chapter> chapterPageInfo = courseService.findChapter(chapterParam,pageNo,pageSize);
        mav.addObject("chapterPage", chapterPageInfo);
        mav.setViewName("backend/back_courseReourceSet");
        return mav;
    }
    @ResponseBody
    @RequestMapping("/addCourse")
    public ResponseResult addCourse(String courseName, String courseInfo, String author, @RequestParam("file") MultipartFile  file, String recommendationGrade, String courseTypeId) {
        ResponseResult result = new ResponseResult();
        try {
            courseService.addCourse(courseName,courseInfo, author, file, recommendationGrade,courseTypeId);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String id, String status) {
        ResponseResult result = new ResponseResult();
        try {
            courseService.modifyStatus(id,status);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
    @ResponseBody
    @RequestMapping("/modifyCourse")
    public ResponseResult modifyCourse(String courseId, String courseName, String author,@RequestParam("file") MultipartFile file, String recommendationGrade, String courseTypeId) {
        ResponseResult result = new ResponseResult();
        try {
            courseService.modifyCourse(courseId,courseName,author,file,recommendationGrade,courseTypeId);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        }catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (CourseExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

}
